<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加图书</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" th:href="@{/static/layui/css/layui.css}">

    <style type="text/css">
        #bookimg{
            width:100px;
            height: 100px;
        }
    </style>
</head>

<body style="width: 800px;height: 600px;">
<!--编辑表单-->
<div id="editBook">
    <form class="layui-form" id="saveBook" style="margin-top:10px">
        <input type="hidden" name="id" id="id">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">图书名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" id="name" required lay-verify="required" autocomplete="off"
                               class="layui-input" placeholder="必填">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">ISBN码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="isbn" id="isbn" required lay-verify="required" autocomplete="off"
                               class="layui-input" placeholder="必填">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="author" id="author" required lay-verify="required" autocomplete="off"
                               class="layui-input" placeholder="必填">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">图书分类</label>
                    <div class="layui-input-inline">
                        <select name="categoryCode" id="categoryCode" lay-verify="required">
                            <option value=""></option>
                        </select>
                        <div class="layui-unselect layui-form-select">
                            <div class="layui-select-title">
                                <input type="text" placeholder="请选择" value="" readonly=""
                                       class="layui-input layui-unselect">
                                <i class="layui-edge"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">出版社</label>
                    <div class="layui-input-inline">
                        <select name="publisherCode" id="publisherCode" lay-verify="required">
                            <option value=""></option>
                        </select>
                        <div class="layui-unselect layui-form-select">
                            <div class="layui-select-title">
                                <input type="text" placeholder="请选择" value="" readonly=""
                                       class="layui-input layui-unselect">
                                <i class="layui-edge"></i>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">语种</label>
                    <div class="layui-input-inline">
                        <select name="languageCode" id="languageCode" lay-verify="required">
                            <option value=""></option>
                        </select>
                        <div class="layui-unselect layui-form-select">
                            <div class="layui-select-title">
                                <input type="text" placeholder="请选择" value="" readonly=""
                                       class="layui-input layui-unselect">
                                <i class="layui-edge"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">存放位置</label>
                    <div class="layui-input-inline">
                        <select name="locationCode" id="locationCode" lay-verify="required">
                            <option value=""></option>
                        </select>
                        <div class="layui-unselect layui-form-select">
                            <div class="layui-select-title">
                                <input type="text" placeholder="请选择" value="" readonly=""
                                       class="layui-input layui-unselect">
                                <i class="layui-edge"></i>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">译者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="translator" id="translator" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">版次</label>
                    <div class="layui-input-inline">
                        <input type="text" name="edition" id="edition"  autocomplete="off"
                               class="layui-input" value="1">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">图书单价</label>
                    <div class="layui-input-inline">
                        <input type="text" name="price" id="price" lay-verify="number" autocomplete="off"
                               class="layui-input" value="0">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">页数</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pages" id="pages" lay-verify="number" autocomplete="off"
                               class="layui-input" value="0">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">字数</label>
                    <div class="layui-input-inline">
                        <input type="text" name="words" id="words" lay-verify="number|integer" autocomplete="off"
                               class="layui-input" value="0">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">藏书数量</label>
                    <div class="layui-input-inline">
                        <input type="text" name="totalNumber" id="totalNumber" lay-verify="number|integer" autocomplete="off"
                               class="layui-input" value="0">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">馆内剩余</label>
                    <div class="layui-input-inline">
                        <input type="text" name="leftNumber" id="leftNumber" lay-verify="number|integer"
                               autocomplete="off"
                               class="layui-input" value="0">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <div class="layui-inline">
                    <label class="layui-form-label">简介</label>
                    <div class="layui-input-block">
                        <textarea name="description" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-inline">
                <label class="layui-form-label">上传图片</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="uploadImage">
                            <i class="layui-icon">&#xe67c;</i>选择文件
                        </button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="bookimg">
                        </div>
                        <input type="text" name="imgPath" id="imgPath" hidden="hidden">
                        <!--<button type="button" class="layui-btn layui-btn-warm" id="uploadBtn">开始上传</button>-->
                    </div>
                </div>
            </div>

            <div class="layui-form-item" style="margin-top:40px" id="check">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="saveBook">保存</button>
                    <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
                </div>
            </div>
    </form>
</div>


<script src="/static/js/jquery-1.11.3.min.js" th:src="@{/static/js/jquery-1.11.3.min.js}"></script>
<script src="/static/layui/layui.all.js" th:src="@{static/layui/layui.all.js}"></script>
<script src="/static/js/util.js" th:src="@{/static/js/util.js}"></script>

<!--ctx-->
<script th:replace="~{fragment::ctx}"/>

<script>
    var element, layer, laydate, form, upload;

    $(function () {
        getList(ctx+'api/bookCategory/list',$("#categoryCode"));
        getList(ctx+'api/bookPublisher/list',$("#publisherCode"));
        getList(ctx+'api/bookShelf/list',$("#locationCode"));
        getList(ctx+'api/bookLanguage/list',$("#languageCode"));

        // 使用模块
        layui.use(['element', 'layer', 'laydate', 'form','upload'], function () {
            element = layui.element;
            layer = layui.layer;
            laydate = layui.laydate;
            form = layui.form;
            upload = layui.upload;
        });

        //普通图片上传
        var uploadInst =upload.render({
            elem: '#uploadImage'
            ,url: ctx+'api/file/uploadImage'
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败',{icon:5,time:1500});
                }else{
                    $('#bookimg').attr('src', ctx+res.imgUrl);
                    $('#imgPath').val(res.imgUrl);
                    return layer.msg('上传成功',{icon:1,time:1500});
                }

            }
            ,error: function(){
                return layer.msg('上传失败',{icon:5,time:1500});
            }
        });

        form.verify({
            integer:[
                /^[0-9]\d*$/
                ,'只能输入整数！'
            ]
        });


        //监听表单提交
        // 修改
        form.on('submit(saveBook)', function (data) {
            //layer.alert(JSON.stringify(data.field));
            data.field.categoryName=$("#categoryCode option:selected").text();
            data.field.languageName=$("#languageCode option:selected").text();
            data.field.locationName=$("#locationCode option:selected").text();
            data.field.publisherName=$("#publisherCode option:selected").text();
            $.ajax({
                url: ctx+'api/book/save',
                type: 'POST',
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data.field),
                success: function (result) {
                    if (result.code == 200) {
                        // layer.msg("修改成功！", {icon: 6});
                        cleanForm("#saveBook");
                        layer.closeAll();//关闭所有的弹出层
                        top.$(".layui-show").find("iframe")[0].contentWindow.userTable.reload();
                    } else {
                        layer.alert(result.message);
                    }
                }
            });

            return false;

        });
    });


</script>
</body>

</html>